import {NavType, PageConfig} from "plain-micro-application";
import {createReactPageMounter} from "@/micro/app/react/mountReactPage";
import {registryDemoApplication} from "./registryDemoApplication";

async function getPage(pageConfig: PageConfig) {
    let path = pageConfig.path
    if (path.endsWith('.jsx')) {
        path = path.slice(0, path.length - 4)
        return (await import('../pages/' + path + '.jsx')).default
    }
    if (path.endsWith('.tsx')) {
        path = path.slice(0, path.length - 4)
    }
    return (await import('../pages/' + path + '.tsx')).default
}

export function registryBasePages(nav: NavType) {
    nav.micro.registry({
        name: 'BASE自身微应用模块',
        type: 'React',
        pattern: [
            'dashboard',
            'detail',
            'form',
            'list',
            'result',
            'user',
            'test',
            'other',
        ],
        loader: async (data) => {
            const {page,} = data
            const Page = await getPage(page)
            const {mount, unmount} = createReactPageMounter({loaderData: data, Page})
            mount()
            return unmount
        },
    })
    registryDemoApplication(nav)
}
